﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace AdornerPopup.Core.Controls
{
    [TemplatePart(Name = EditableTextBoxTemplateName, Type = typeof(TextBox))]
    public class WapComboBox : ListBox
    {
        #region Constants
        private const string EditableTextBoxTemplateName = "PART_EditableTextBox";
        private const string ToggleButtonTtemplateName = "toggleButton";
        #endregion

        #region Fields
        private ToggleButton _toggleButton;
        private TextBox _editableTextBoxSite;
        #endregion

        #region Private Properties
        private ToggleButton ToggleButton
        {
            get { return _toggleButton; }
            set
            {
                _toggleButton = value;
            }
        }


        private TextBox EditableTextBoxSite
        {
            get { return _editableTextBoxSite; }
            set { _editableTextBoxSite = value; }
        }
        #endregion

        #region Dependency Properties

        /// <summary>
        /// <see cref="IsDropDownOpenProperty"/>  依赖属性
        /// </summary>
        public static readonly DependencyProperty IsDropDownOpenProperty =
            DependencyProperty.Register("IsDropDownOpen", typeof(bool), typeof(WapComboBox), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits, IsDropDownOpenChanged, null, true, System.Windows.Data.UpdateSourceTrigger.PropertyChanged));

        private static void IsDropDownOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((WapComboBox)d).IsDropDownOpenChanged((bool)e.NewValue, (bool)e.OldValue);
        }

        /// <summary>
        /// <see cref="MaxDropDownHeightProperty"/> 依赖属性
        /// </summary>
        public static readonly DependencyProperty MaxDropDownHeightProperty =
            DependencyProperty.Register("MaxDropDownHeight", typeof(double), typeof(WapComboBox), new PropertyMetadata(double.NaN));

        /// <summary>
        /// <see cref="TextProperty"/> 依赖属性
        /// </summary>
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(WapComboBox), new PropertyMetadata(string.Empty));

        /// <summary>
        /// <see cref="SelectionBoxItemProperty"/> 依赖属性
        /// </summary>
        public static readonly DependencyProperty SelectionBoxItemProperty =
            DependencyProperty.Register("SelectionBoxItem", typeof(object), typeof(WapComboBox), new PropertyMetadata(null));

        /// <summary>
        /// <see cref="SelectionBoxItemStringFormatProperty"/> 依赖属性
        /// </summary>
        public static readonly DependencyProperty SelectionBoxItemStringFormatProperty =
            DependencyProperty.Register("SelectionBoxItemStringFormat", typeof(string), typeof(WapComboBox), new PropertyMetadata(string.Empty));

        /// <summary>
        /// <see cref="SelectionBoxItemTemplateProperty"/> 依赖属性
        /// </summary>
        public static readonly DependencyProperty SelectionBoxItemTemplateProperty =
            DependencyProperty.Register("SelectionBoxItemTemplate", typeof(DataTemplate), typeof(WapComboBox), new PropertyMetadata(null));

        /// <summary>
        /// <see cref="IsEditableProperty"/> 依赖属性
        /// </summary>
        public static readonly DependencyProperty IsEditableProperty =
            DependencyProperty.Register("IsEditable", typeof(bool), typeof(WapComboBox), new PropertyMetadata(false));

        #endregion

        #region Dependency Properties Wrapper

        /// <summary>
        /// 获取或设置 下拉框最大高度
        /// </summary>
        public double MaxDropDownHeight
        {
            get { return (double)GetValue(MaxDropDownHeightProperty); }
            set { SetValue(MaxDropDownHeightProperty, value); }
        }

        /// <summary>
        /// 获取或设置 是否打开下拉框
        /// </summary>
        public bool IsDropDownOpen
        {
            get { return (bool)GetValue(IsDropDownOpenProperty); }
            set { SetValue(IsDropDownOpenProperty, value); }
        }

        /// <summary>
        /// 获取或设置 选中框项
        /// </summary>
        public object SelectionBoxItem
        {
            get { return (object)GetValue(SelectionBoxItemProperty); }
            set { SetValue(SelectionBoxItemProperty, value); }
        }

        /// <summary>
        /// 获取或设置 选中框项格式化
        /// </summary>
        public string SelectionBoxItemStringFormat
        {
            get { return (string)GetValue(SelectionBoxItemStringFormatProperty); }
            set { SetValue(SelectionBoxItemStringFormatProperty, value); }
        }

        /// <summary>
        /// 获取或设置 选中框项模板
        /// </summary>
        public DataTemplate SelectionBoxItemTemplate
        {
            get { return (DataTemplate)GetValue(SelectionBoxItemTemplateProperty); }
            set { SetValue(SelectionBoxItemTemplateProperty, value); }
        }

        /// <summary>
        /// 获取或设置 是否能够编辑
        /// </summary>
        public bool IsEditable
        {
            get { return (bool)GetValue(IsEditableProperty); }
            set { SetValue(IsEditableProperty, value); }
        }

        /// <summary>
        /// 获取或设置 文本
        /// </summary>
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }
        #endregion

        #region Constructors
        static WapComboBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WapComboBox), new FrameworkPropertyMetadata(typeof(WapComboBox)));
        }
        #endregion


        #region Override Mehtods
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.ToggleButton = GetTemplateChild(ToggleButtonTtemplateName) as ToggleButton;
            this.EditableTextBoxSite = GetTemplateChild(EditableTextBoxTemplateName) as TextBox;
        }

        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            base.OnSelectionChanged(e);

            SelectedItemUpdated();

            if (this.IsEditable)
            {
                EditableTextBoxSite.Text = this.Text;
            }
            else
            {
                SelectionBoxItem = this.SelectedItem;
            }
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new WapComboBoxItem();
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return item is WapComboBoxItem;
        }

        #endregion

        #region Event Handlers

        private void SelectedItemUpdated()
        {
            string text = SelectedItem?.ToString() ?? string.Empty;

            if (Text != text)
            {
                SetCurrentValue(TextProperty, text);
            }
        }

        private void IsDropDownOpenChanged(bool newValue, bool oldValue)
        {
            if (ToggleButton == null) return;

            ToggleButton.SetCurrentValue(ToggleButton.IsCheckedProperty, newValue);
        }

        #endregion



    }
}
